package com.leetcode;

import java.util.ArrayList;
import java.util.List;


public class L0653 {
    private List<Integer> list;
    public boolean findTarget(TreeNode root, int k) {
        if (root==null)
            return false;
        list=new ArrayList<>();
        find(root,k);
        int i=0,j=list.size()-1;
        while (i<j){
            if (list.get(i)+list.get(j)==k)
                return true;
            else if (list.get(i)+list.get(j)>k){
                j--;
            }else {
                i++;
            }
        }
        return false;
    }

    public void find(TreeNode root,int k){
        if (root.left!=null)
            find(root.left,k);
        list.add(root.val);
        if (root.right!=null)
            find(root.right,k);
    }
}
